//
//  VideoDetailViewController.m
//  Delicious2
//
//  Created by FSD on 16/1/8.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "VideoDetailViewController.h"
#import "UMSocial.h"
@interface VideoDetailViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) UIBarButtonItem *item1;
@property (nonatomic, strong) UIBarButtonItem *item2;
@property (nonatomic, retain) UIBarButtonItem *item3;
@property (nonatomic, strong) NSArray *array;
@property (nonatomic, strong) DataBaseHandle *dataBaseHandle;
@property (nonatomic, assign) BOOL save;
@property (nonatomic, strong) NSString *titleStr;
@property (nonatomic, strong) MBProgressHUD *progressHud;
@property (nonatomic, strong) UIView *nightView;


@end

@implementation VideoDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = self.titleName;
    self.save = NO;
    self.item1 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"SaveImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightCollectionDidPress:)];
    self.item2 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"HaveSaveImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStylePlain target:self action:@selector(rightHaveCollectionDidPress:)];
    
    self.item3 = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"FenxiangImage"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]style:UIBarButtonItemStyleDone target:self action:@selector(rightButton:)];
    self.nightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    self.nightView.alpha = 0.5;
    self.nightView.userInteractionEnabled = YES;
//    self.nightView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:self.nightView];
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];

    self.array = [self.dataBaseHandle selectDataBase:nil];
    
    
    if (self.urlId == nil) {
        if (self.save) {
            self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
        } else {
            self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
        }
        for (NSDictionary *dic in self.array) {
            self.titleStr = dic[@"title"];
            NSArray *array = [self.titleName componentsSeparatedByString:@"-"];
            NSLog(@"%@", array);
            if ([self.titleStr isEqualToString:array[1]]) {
                self.save = YES;
                
            } else {
                self.save = NO;
            }
        if (self.save) {
            self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
        } else {
            self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
        }
    }
} else {
        for (NSDictionary *dic in self.array) {
            self.titleStr = dic[@"title"];
            NSLog(@"呵呵呵%@", dic[@"title"]);
            if ([self.titleStr isEqualToString:self.titleName]) {
                self.save = YES;

            } else {
                self.save = NO;
            }
        }
        if (self.save) {
            self.navigationItem.rightBarButtonItems = @[self.item2,self.item3];
        } else {
            self.navigationItem.rightBarButtonItems = @[self.item1,self.item3];
        }
    }
    
    [self.dataBaseHandle closeDataBase];

    [self createView];
}


- (void)createView {
    NSString *str = [NSString stringWithFormat:@"http://h5.izhangchu.com/course/view.html?&series_id=%@", self.urlId];
    self.webView = [[UIWebView alloc]initWithFrame:CGRectMake(0, -(self.view.frame.size.height / 15), self.view.frame.size.width, self.view.frame.size.height + self.view.frame.size.height / 10)];
    self.webView.delegate = self;
    self.webView.scrollView.bounces = NO;
    if (self.urlId == nil) {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:self.urlStr]];
        [self.webView loadRequest:request];
    } else {
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:str]];
        [self.webView loadRequest:request];
    }
    [self.view addSubview:self.webView];
//    [self.view addSubview:self.nightView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.dataBaseHandle = [DataBaseHandle shareDataBase];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    
    self.array = [self.dataBaseHandle selectDataBase:nil];
    
    
    if (self.urlId == nil) {
        if (self.save) {
            self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
        } else {
            self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
        }
        for (NSDictionary *dic in self.array) {
            self.titleStr = dic[@"title"];
            NSArray *array = [self.titleName componentsSeparatedByString:@"-"];
            NSLog(@"%@", array);
            if ([self.titleStr isEqualToString:array[1]]) {
                self.save = YES;
                
            } else {
                self.save = NO;
            }
            if (self.save) {
                self.navigationItem.rightBarButtonItems = @[self.item2, self.item3];
            } else {
                self.navigationItem.rightBarButtonItems = @[self.item1, self.item3];
            }
        }
    } else {
        for (NSDictionary *dic in self.array) {
            self.titleStr = dic[@"title"];
            NSLog(@"呵呵呵%@", dic[@"title"]);
            if ([self.titleStr isEqualToString:self.titleName]) {
                self.save = YES;
                
            } else {
                self.save = NO;
            }
        }
        if (self.save) {
            self.navigationItem.rightBarButtonItems = @[self.item2,self.item3];
        } else {
            self.navigationItem.rightBarButtonItems = @[self.item1,self.item3];
        }
    }
    
    [self.dataBaseHandle closeDataBase];
    
    [self.nightView nightMode];
}

- (void)webViewDidStartLoad:(UIWebView *)webView {
    self.progressHud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    self.progressHud.labelText = @"正在为您加载中...";
    self.progressHud.animationType = MBProgressHUDAnimationFade;
    [self.progressHud hide:YES afterDelay:3.0];
}


//- (void)webViewDidFinishLoad:(UIWebView *)webView {
//    [self.progressHud hide:YES];
//}

- (void) webViewDidFinishLoad:(UIWebView *)webView
{
    
//    [self.progressHud hide:YES];
//    NSString *night = [[NSUserDefaults standardUserDefaults] objectForKey:@"NightMode"];
//    //    判断夜间模式是否已经开启
//    if ([night
//         isEqualToString:@"Night"]) {
//        self.navigationController.navigationBar.barTintColor = [UIColor blackColor];
//        
//        // 字体颜色
//        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'white'"];
//        // 背景色
//        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#2E2E2E'"];
//        
//    } else {
//        
//        // 字体颜色
//        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.webkitTextFillColor= 'black'"];
//        // 背景色
//        [webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('body')[0].style.background='#FFFAFA'"];
//        
//    }
}

#pragma mark - 收藏事件
- (void)rightCollectionDidPress:(id)sender {

    NSString *urlString = [NSString stringWithFormat:@"http://h5.izhangchu.com/course/view.html?&series_id=%@", self.urlId];
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    if (self.urlId != nil) {
        NSArray *titleArray;
        titleArray = [self.dataModel.series_name componentsSeparatedByString:@"#"];
        self.titleStr = titleArray[1];
        [self.dataBaseHandle insertDataBase:titleArray[1] withImage:self.dataModel.image withURL:urlString withKind:self.kind];
    } else {
        NSArray *titleArray = [self.topModel.banner_title componentsSeparatedByString:@"-"];
        self.titleStr = titleArray[1];
        NSLog(@"%@", titleArray);
        [self.dataBaseHandle insertDataBase:titleArray[1] withImage:self.topModel.banner_picture withURL:self.topModel.banner_link withKind:self.kind];
        NSLog(@"标题%@， 图片%@， url%@， 类型%@", titleArray[1], self.topModel.banner_picture, self.topModel.banner_link, self.kind);
    }
    
    [self.dataBaseHandle selectDataBase:nil];
    [self.dataBaseHandle closeDataBase];
    
    
    
    self.navigationItem.rightBarButtonItems = @[self.item2,self.item3];

    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode = MBProgressHUDModeText;
    HUD.margin = 10;
    HUD.cornerRadius = 3;
    HUD.labelText = @"收藏成功";
    [HUD hide:YES afterDelay:1];
}

- (void)rightHaveCollectionDidPress:(id)sender {
    
    [self.dataBaseHandle openDataBase];
    [self.dataBaseHandle createTable];
    [self.dataBaseHandle deleteDataBase:self.titleStr];
    [self.dataBaseHandle closeDataBase];
    
    self.navigationItem.rightBarButtonItems = @[self.item1,self.item3];
    
    MBProgressHUD *HUD = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    HUD.mode = MBProgressHUDModeText;
    HUD.margin = 10;
    HUD.cornerRadius = 3;
    HUD.labelText = @"取消收藏";
    [HUD hide:YES afterDelay:1];
}

#pragma mark - 分享按键事件
- (void)rightButton:(id)sender {
    NSString *shareUrl;
    if (self.urlId == nil) {
        shareUrl = self.topModel.banner_link;
    } else {
        NSString *urlString = [NSString stringWithFormat:@"http://h5.izhangchu.com/course/view.html?&series_id=%@", self.urlId];
        shareUrl = urlString;
    }
    [UMSocialSnsService presentSnsIconSheetView:self appKey:@"507fcab25270157b37000010" shareText:shareUrl  shareImage:nil shareToSnsNames:[NSArray arrayWithObjects:UMShareToSina,UMShareToTencent, nil] delegate:nil];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
